package org.ztest.spring.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyInterceptor
{
    @Pointcut("execution(public * org.ztest.spring.aop.AopInterfaceImpl.*(..))")
    private void anyMethod()
    {
        System.out.println("MyInterceptor:anyMethod");
    }
    
    @Before("anyMethod()")
    public void doBefore()
    {
        System.out.println("MyInterceptor:doBefore");
    }
    
    @AfterReturning("anyMethod()")
    public void doAfter()
    {
        System.out.println("MyInterceptor:doAfter");
    }
    
    @After("anyMethod()")
    public void after()
    {
        System.out.println("MyInterceptor:after");
    }
    
    @AfterThrowing("anyMethod()")
    public void doAfterThrow()
    {
        System.out.println("MyInterceptor:doAfterThrow");
    }
    
    @Around("execution(* org.ztest.spring.aop.AopInterfaceImpl.*(..))")
    public void doAround(ProceedingJoinPoint joinPoint) throws Throwable
    {
        long start = System.currentTimeMillis();
        Object ret = joinPoint.proceed();
        System.out.println(ret);
        System.out.println(joinPoint.getSignature());
        System.out.println(joinPoint.getArgs());
        System.out.println("Cost " + (System.currentTimeMillis() - start) + "ms");
        System.out.println("MyInterceptor:doAround");
    }
    
}
